/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Pagos.Herramientas.Generales;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author FCCCRTI05
 */
public class ComprobarConexion extends HttpServlet
{

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        PrintWriter out = response.getWriter();
        //REINICIO DEL BUFFER
        response.reset();
        response.resetBuffer();
        response.setContentType("text/xml; charset=ISO-8859-1");
        response.setHeader("Cache-Control","no-cache");
        try
        {
            //CONEXION A LA BD
            db.Conexion conectar = (db.Conexion)request.getSession().getAttribute("db");
            if (conectar != null)
            {
                Connection conex = conectar.ObtenerConexionSQLServer();
                //SALIDA XML
                if (conex.isClosed() == false)
                {
                    out.write("<datos>");
                    out.write("<valido>Si</valido>");
                    out.write("<activa>Si</activa>");
                    out.write("</datos>");
                }
                if (conex.isClosed() == true)
                {
                    out.write("<datos>");
                    out.write("<valido>No</valido>");
                    out.write("<activa>No</activa>");
                    out.write("</datos>");
                }
            }
            if (conectar == null)
            {
                out.write("<datos>");
                out.write("<valido>No</valido>");
                out.write("<activa>No</activa>");
                out.write("</datos>");
            }
        }
        catch (Exception x)
        {
            out.write("<datos>");
            out.write("<valido>No</valido>");
            out.write("<activa>"+x.getMessage()+"</activa>");
            out.write("</datos>");
        }
        finally
        {
            out.flush();
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        processRequest(request, response);
    }
}